﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace mvchotleweb.Controllers
{
    public class RoomManageController : Controller
    {
        //
        // GET: /RoomManage/

        public ActionResult booking()
        {
            hotel.BLL.tb_booking bllbooking = new hotel.BLL.tb_booking();
            hotel.Model.tb_booking modbooking = new hotel.Model.tb_booking();
            modbooking.name = Request.Form["name"];
            modbooking.email = Request.Form["email"];
            modbooking.phone = Request.Form["phone"];
            modbooking.room =Convert.ToInt32(Request.Form["room"]);
            DateTime dt = DateTime.Now;
            string day = Request.Form["day"];
            string moth = Request.Form["moth"];
            string dateString = day + "/" + moth + "/" + dt.Year.ToString();
            modbooking.time = Convert.ToDateTime(dateString);
            modbooking.context = Request.Form["context"];
            modbooking.chekin = false;
            modbooking.is_chack = false;

            if (bllbooking.Add(modbooking) != 0)
                return Content("<Script>alert('预定成功');window.location.href='../Home';</script>");
            return Content("<Script>alert('预定失败');window.location.href='../Home';</script>");
        }


        public ActionResult GetRoomTypeList()
        {
            hotel.BLL.View_RoomsType fdll = new hotel.BLL.View_RoomsType();
            string res = JsonConvert.SerializeObject(fdll.GetModelList(""));
            return Content(res);
        }

        public ActionResult addTypeRoom()
        {
            string filename = "123";
            string path;
            hotel.BLL.tb_roomtype rombll = new hotel.BLL.tb_roomtype();
            hotel.BLL.tb_Image imgbll = new hotel.BLL.tb_Image();
            hotel.Model.tb_roomtype modrom = new hotel.Model.tb_roomtype();
            hotel.Model.tb_Image imgmod = new hotel.Model.tb_Image();

            modrom.typename = Request.Form["roomtypeName"];
            modrom.rooms = Convert.ToInt32(Request.Form["roomnum"]);
            modrom.Price = Convert.ToInt32(Request.Form["price"]);
            modrom.bednum = Convert.ToInt32(Request.Form["bednum"]);

            foreach (string sfile in Request.Files)
            {
                HttpPostedFileBase file = Request.Files[sfile];
                if (file == null)
                    continue;
                string miniType = file.ContentType;
                Stream fileStream = file.InputStream;
                path = Server.MapPath("~/Images");
                filename = Path.GetFileName(file.FileName);
                file.SaveAs(Path.Combine(path, filename));
            }
            if (filename == "123")
                return Content("<Script>alert('添加失败')</script>");
            imgmod.url = "/Images/" + filename;
            modrom.ImageID = imgbll.Add(imgmod);
            rombll.Add(modrom);
            return Content("<Script>alert('添加传成功')</script>");
        }

        public ActionResult delRoomtype()
        {
            int id = Convert.ToInt32(Request.Form["ID"]);
            hotel.BLL.tb_roomtype rombll = new hotel.BLL.tb_roomtype();
            bool res = rombll.Delete(id);
            return Content(res.ToString());           
        }



        #region  留言管理

        public ActionResult addMessage()
        {
            hotel.BLL.tb_message bllmessge = new hotel.BLL.tb_message();
            hotel.Model.tb_message modmessage = new hotel.Model.tb_message();
            modmessage.Name = Request.Form["name"];
            modmessage.addr = Request.Form["addr"];
            modmessage.email= Request.Form["email"];
            modmessage.context = Request.Form["context"];
            modmessage.posttime = DateTime.Now;
            if (bllmessge.Add(modmessage)>0)
                return Content("<Script>alert('添加成功');javascript:history.go(-1);</script>");
            else
                return Content("<Script>alert('添加失败！！请确认填写了姓名·电话·地址等信息');javascript:history.go(-1);</script>");
        }


        #endregion  留言管理
    }
}
